import { call, put, takeLatest } from 'redux-saga/effects';
import { USER_LOGIN,loging } from '../actions/login.action';
import Message from '../components/SimpleLayer/Message';
import API from '../services';
 
function showErr(content){
	Message.error({
		top:'6px',
		width:'300',
		content,
		delayAutoClose:2000,
		yes:false
	});
}
function* Login(action) {
   try {
      	const res = yield call(API.Login, action.data);
      	yield put(loging(false));
  		if (res.success) {
  			localStorage.setItem('e_book_user',JSON.stringify(Object.assign(res.userInfo,{token:res.result})));
			if (action.data.isRemember) {
				localStorage.setItem('e_book_userinfo',JSON.stringify(action.data));
			}else{
				localStorage.removeItem('e_book_userinfo');
			}
			window.location.href='#/manage';
		}else{
			showErr(res.result);
		}
   } catch (e) {
      	showErr(e.message);
   }
}

function* LoginSaga() {
  yield takeLatest(USER_LOGIN, Login);
}

export default LoginSaga;